package com.jintoufs.service.category.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.category.CategoryMapper;
import com.jintoufs.domain.category.Category;
import com.jintoufs.logAnnotation.SystemServiceLog;
import com.jintoufs.service.category.CategoryService;
import com.jintoufs.util.resource.Node;
import com.jintoufs.util.resource.ResourceNodeTree;
import com.jintoufs.util.ztree.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Category record) {
        return categoryMapper.insert(record);
    }

    @Override
    public int insertSelective(Category record) {
        return categoryMapper.insertSelective(record);
    }

    @Override
    public Category selectByPrimaryKey(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Category record) {
        return categoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Category record) {
        return categoryMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Category> queryByParams(Map map) {
        return categoryMapper.queryByParams(map);
    }


    @Override
    public  List<Category> queryByParam(Map map) {
        return categoryMapper.queryByParam(map);
    }

    @Override
    public Map<String, Object> save(Category category) {
        String parentIds;
        if (category.getParentId()==0){
            parentIds="0/";
        }else {
            parentIds = categoryMapper.selectByPrimaryKey(category.getParentId()).getParentIds()+category.getParentId()+"/";
        }
        category.setParentIds(parentIds);
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (category.getId() == null) {
                categoryMapper.insertSelective(category);
                result.put("msg", "新增成功");
            } else {
                categoryMapper.updateByPrimaryKeySelective(category);
                result.put("msg", "更新成功");
            }
            result.put("status", "OK");
            return result;
        } catch (Exception e) {
            result.put("msg", "操作失败");
            result.put("status", "ERROR");
            return result;
        }
    }

    @Override
    @SystemServiceLog(description = "删除酒店")
    public void delete(Integer id) {
            categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Category> getByFilter(QueryFilters filters) {
        List<Category> result = categoryMapper.getByFilter(filters);
        return result;
    }

    @Override
    public Category getById(Integer id) {
        return categoryMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<Object> getResourceTreeList(List<Category> entity) {
        List<Category> resourceList = (List<Category>) ((ArrayList) entity).clone();
        List<Category> list = new ArrayList<>();
        List<Node> nodes = new ArrayList<>();
        ResourceNodeTree resourceTree = new ResourceNodeTree();
        Map<Integer, Category> map = new ConcurrentHashMap<>();
        while (resourceList.size() != 0) {
            Iterator<Category> iterator = resourceList.iterator();
            while (iterator.hasNext()) {
                Category category = iterator.next();
                if (category.getParentId() == 0) {
                    list.add(category);
                    map.put(category.getId(),category);
                    iterator.remove();
                } else if (map.containsKey(category.getParentId())) {
                    list.add(category);
                    map.put(category.getId(),category);
                    iterator.remove();
                }
            }
        }
        for(Category category:list){
            nodes.add(new Node(category.getId(),category.getParentId(),category));
        }
        ResourceNodeTree resourceNodeTree = resourceTree.createTree(nodes);
        resourceTree.firstOrder(resourceNodeTree.getRoot());
        return resourceTree.getFristList();
    }
    @Override
    public List<TreeNode> getModuleTree(Category entity) {
        List<Category> list = categoryMapper.queryZtree();
        List<TreeNode> nodes = new ArrayList<TreeNode>();
        for (Category tmp : list) {
            nodes.add(buildModuleNode(tmp, entity));
        }
        return nodes;
    }

    private TreeNode buildModuleNode(Category entity, Category oldResource) {
        TreeNode areaNode = new TreeNode();
        areaNode.setId(String.valueOf(entity.getId()));
        areaNode.setName(entity.getName());
        if (entity.getParentId() != null && entity.getParentId() != 0) {
            areaNode.setPId(String.valueOf(entity.getParentId()));
        } else {
            areaNode.setIsParent(true); // 处理无子节点的父节点
        }
        if (oldResource != null && oldResource.getId().equals(entity.getId())) {
            areaNode.setChecked(true);
        }
        areaNode.setOpen(false);
        return areaNode;
    }
}
